package guitest;

public class RegisterWindow extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;
	
	core.ProcessorClass processor = null;

	public RegisterWindow(core.ProcessorClass p){
		this.processor = p;
		this.init();
	}
	protected void prepare_exit(){
		//Operations to prepare exit of the program
		System.exit(0);
	}
	private void init(){
		this.setSize(new java.awt.Dimension(120,300));
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.out.println("finestra tancada");
				prepare_exit();
			}
		});
		this.setTitle("SEP16 Simulator");
		this.setContentPane(this.getMemoryPanel());
		this.setVisible(true);
	}

	//RegisterPanel
	javax.swing.JTextField registerField = null;
	javax.swing.JList registerList = null;
	javax.swing.JPanel registerPanel = null;
	short lastOffset = 0x0000;
	short nValues = 30;
	public void reloadListValues(){
		this.registerList.setListData(this.processor.rb.getRegisterVector());
	}

	private javax.swing.JComponent getRegisterList(){
		this.registerList = new javax.swing.JList();
		this.registerList.setCellRenderer(new guitest.RegisterWindow.RegisterRender());
		return(this.registerList);
	}
	private javax.swing.JPanel getMemoryPanel(){
		this.registerPanel = new javax.swing.JPanel(new java.awt.BorderLayout());
		this.registerPanel.add(this.getRegisterList(), java.awt.BorderLayout.CENTER);
		return(this.registerPanel);
	}

	class RegisterRender extends javax.swing.JPanel implements javax.swing.ListCellRenderer{
		static final long serialVersionUID = 0;
		javax.swing.JTextField t = new javax.swing.JTextField();
		javax.swing.JTextField pos = new javax.swing.JTextField();

		java.lang.Short regValue = null;
		java.lang.Short posValue = null;
		public java.awt.Component getListCellRendererComponent(javax.swing.JList list, java.lang.Object value, int index, boolean selected, boolean focused) {
			//Extremely dirty trick for java: two shorts are encoded into a Integer
			java.lang.Integer aux = (java.lang.Integer) value;
			regValue = (short)(aux.intValue() & 0x0000FFFF);
			posValue = (short)((aux.intValue() & 0xFFFF0000)>>16);

			//this.setLayout(new java.awt.GridLayout(0,2));
			this.setLayout(new java.awt.BorderLayout());
			t.setText(String.format("0x%04X", regValue.shortValue()));
			t.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
			t.setBorder(null);
			pos.setBorder(null);
			pos.setText(String.format("%d", posValue.shortValue()));
			pos.setBackground(java.awt.Color.lightGray);
			pos.setSize(pos.getWidth() + 40, pos.getHeight());
			this.add(pos, java.awt.BorderLayout.WEST);
			this.add(t, java.awt.BorderLayout.CENTER);
			return(this);
		}
	}

	public static void main(String args[]){
		core.ProcessorClass p = new core.ProcessorClass();
		guitest.RegisterWindow rw =	new RegisterWindow(p);
		p.loadFile(new java.io.File("prog1.s16"));
		p.run();
		rw.registerList.setListData(p.rb.getRegisterVector());
	}
}
